﻿/* 2013/1/10 */
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Cosmos.Media.Play
{
    /// <summary>
    /// Represents a collection of objects of PlayListItem class.
    /// </summary>
    ///
    public class PlayListItemCollection : IEnumerable<PlayListItem>, ICollection<PlayListItem>, IList<PlayListItem>
    {
        private Collection<PlayListItem> _items;

        private PlayList _list;

        /// <summary>
        /// Initializes a new instance of PlayListItemCollection class.
        /// </summary>
        protected internal PlayListItemCollection(PlayList list)
        {
            _items = new Collection<PlayListItem>();

            _list = list;
        }

        public void Add(PlayListItem item)
        {
            item.List = _list;
            _items.Add(item);
        }

        public void AddRange(IEnumerable<PlayListItem> items)
        {
            foreach (PlayListItem anItem in items)
            {
                anItem.List = _list;
                _items.Add(anItem);
            }
        }

        public void Clear()
        {
            _items.Clear();
        }

        public bool Contains(PlayListItem item)
        {
            return _items.Contains(item);
        }

        public void CopyTo(PlayListItem[] array, int index)
        {
            _items.CopyTo(array, index);
        }

        public IEnumerator<PlayListItem> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        public int IndexOf(PlayListItem item)
        {
            return _items.IndexOf(item);
        }

        public void Insert(int index, PlayListItem item)
        {
            item.List = _list;
            _items.Insert(index, item);
        }

        public bool Remove(PlayListItem item)
        {
            if (_items.Contains(item))
            {
                item.List = null;
            }
            return _items.Remove(item);
        }

        public void RemoveAt(int index)
        {
            if (index >= 0 && index < _items.Count)
            {
                PlayListItem item = _items[index];
                item.List = null;
            }
            _items.RemoveAt(index);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)_items.GetEnumerator();
        }

        public int Count
        {
            get
            {
                return _items.Count;
            }
        }

        bool ICollection<PlayListItem>.IsReadOnly
        {
            get
            {
                return ((ICollection<PlayListItem>)_items).IsReadOnly;
            }
        }

        public PlayListItem this[int index]
        {
            get
            {
                return _items[index];
            }

            set
            {
                _items[index] = value;
            }
        }

        public PlayList List
        {
            get
            {
                return _list;
            }
        }

        protected Collection<PlayListItem> Items
        {
            get
            {
                return _items;
            }

            set
            {
                _items = value;
            }
        }
    }
}
